<template>
  <div class="login">
    <el-form :model="loginForm" class="myform" :rules="loginFormRules" ref="loginFormRef">
      <h3>iHRM后台登录系统</h3>
      <el-form-item label="" prop="mobile">
        <el-input prefix-icon="el-icon-user-solid" v-model="loginForm.mobile" />
      </el-form-item>
      <el-form-item label="" prop="password">
        <el-input prefix-icon="el-icon-lock" show-password v-model="loginForm.password" />
      </el-form-item>
      <el-form-item>
        <el-button class="btn" @click="login">登录</el-button>
      </el-form-item>
      <el-link>还没有账号?立即注册</el-link>
    </el-form>
  </div>
</template>
<script>
// import { loginApi } from '@/api/user.js'
import { mapActions } from 'vuex'
export default {
  data() {
    return {
      loginForm: {
        mobile: '13800000002',
        password: '123456'
      },
      loginFormRules: {
        mobile: [
          { required: true, message: '电话不能为空', trigger: 'blur' },
          { pattern: /^1([38]\d|5[0-35-9]|7[3678])\d{8}$/, message: '电话号码格式不正确', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 3, max: 6, message: '密码长度为3-6', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapActions({
      vuexLogin: 'user/loginApi'
    }),
    login() {
      // 表单预校验
      this.$refs.loginFormRef.validate(async result => {
        if (result) {
          try {
            await this.vuexLogin(this.loginForm)
            this.$router.push('/')
          } catch (err) {
            // 捕获错误信息
            // 打印错误信息
            console.log(err.message)
          }
        } else {
          console.log('验证失败')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  background-image: url('../../assets/404_images/common/login.jpg');
  height: 100%;
  background-position: center;
  h3 {
    font-size: 50px;
    font-weight: 200;
    letter-spacing: 7px;
    color: #fff;
  }
  .myform {
    width: 520px;
    height: 400px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

    .btn {
      width: 100%;
      background-color: #4080fe;
      border: none;
      color: #fff;
      font-size: 20px;
      height: 50px;
    }
  }
}
::v-deep input.el-input__inner {
  height: 50px;
}
::v-deep span.el-link--inner {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  color: #fff;
}
::v-deep input.el-input__inner {
  background-color: #d4e5ff;
}
::v-deep .el-form-item__error {
  color: #fff;
}
</style>
